//
//  PackageDetailsViewController.m
//  UI_HLJ_Frame
//
//  Created by dllo on 15/7/7.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "PackageDetailsViewController.h"

#import "PackageDetaileModel.h"
#import "PackageMerchantDetaileModel.h"
#import "MerchantDetailePackageModel.h"

#import "UIImageView+WebCache.h"
#import "HTTPTool.h"
#import "AFNetworking.h"
#import "weddingThingModel.h"

#import "MerchantTableViewCell.h"
#import "DetailsTableViewCell.h"
#import "BuyTableViewCell.h"

#import "MBProgressHUD.h"

#import "MerchantDetailsViewController.h"


#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface PackageDetailsViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property(nonatomic,retain)UITableView * tableView;
@property(nonatomic,retain)UIScrollView *  scrollView;
@property(nonatomic,retain)UIView *headerView;

@property(nonatomic, retain)NSMutableArray *lunboArr;
@property(nonatomic,retain)NSMutableArray *lunbotupicArr;
@property(nonatomic, retain)NSMutableArray *merchantArr;
// 套餐描述的自适应
@property(nonatomic, retain)NSMutableArray *ziArr;

// 标题View
@property(nonatomic, retain)UIView *titleView;

@property(nonatomic, retain)UILabel *packageTitle;
@property(nonatomic, retain)UILabel *packageStytle;
@property(nonatomic, retain)UILabel *leftPackagePreferential;
@property(nonatomic, retain)UILabel *rightPackagePreferential;
// 套餐描述
@property(nonatomic, retain)NSDictionary *Dic;
// 商家的名
@property(nonatomic, retain)NSDictionary *nameDic;
// 商家的地址
@property(nonatomic, retain)NSDictionary *addressDic;
// 商家的图的数组
@property(nonatomic, retain)NSMutableArray *shopArr;
// id
@property(nonatomic, retain)NSDictionary *bidDic;
@property(nonatomic,retain)NSDictionary *bidmerchantdic;
// 跳转到商家页面上的最上边的图
@property(nonatomic, retain)NSDictionary *merchantDic;

// 轮播图下得view
@property(nonatomic, retain)NSDictionary *titleDic;
@property(nonatomic, retain)NSDictionary *styleDic;
@property(nonatomic, copy)NSString *titleStr;
@property(nonatomic, copy)NSString *styleStr;

// 小菊花
@property(nonatomic,retain)MBProgressHUD *hud;
//设置刷新当前页数
//@property(nonatomic,assign)NSInteger pageCount;

@end

@implementation PackageDetailsViewController

- (void)dealloc
{
    [_idStr release];
    [_tableView release];
    [_scrollView release];
    [_headerView release];
    [_lunboArr release];
    [_lunbotupicArr release];
    [_merchantArr release];
    [_merchantDic release];
    [_ziArr release];
    [_titleView release];
    [_titleStr release];
    [_titleDic release];
    [_packageStytle release];
    [_packageTitle release];
    [_leftPackagePreferential release];
    [_rightPackagePreferential release];
    [_Dic release];
    [_nameDic release];
    [_addressDic release];
    [_shopArr release];
    [_bidDic release];
    [_bidmerchantdic release];
    [_styleDic release];
    [_styleStr release];
    
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.919 blue:0.211 alpha:1.000];
   
     // 导航栏不透明
    self.navigationController.navigationBar.translucent=NO;
    
    
    
    
    [self createData];
    
    self.title = @"💎 套 餐 💎";
    
    UIBarButtonItem *bar=[[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStylePlain target:nil action:nil];
//    [bar release];
    self.navigationItem.backBarButtonItem=bar;
    
    self.tableView = [[UITableView alloc ]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49-64) style:UITableViewStylePlain];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
//    [self.tableView release];
    //不显示cell间分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:self.tableView];
    
    
    self.headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HEIGHT/2)];
//    [self.headerView release];
//    self.headerView.backgroundColor = [UIColor colorWithRed:0.609 green:1.000 blue:0.900 alpha:1.000];
    self.tableView.tableHeaderView = self.headerView;
    [self.tableView addSubview:self.headerView];
    
    self.hud = [[MBProgressHUD alloc] init];
    [self.view addSubview:self.hud];
    [self.hud show:YES];
    self.hud.labelText = @"loading...";
    
    
    
    
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT/3, WIDTH, (HEIGHT/2) - (HEIGHT/3))];
    [self.headerView addSubview:self.titleView];
    [self.titleView release];
//    self.titleView.backgroundColor = [UIColor yellowColor];

    
    [self createLaber];
    
//    self.pageCount = 2;
    
//    self.hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
//    self.hud.labelText =@"loading...";
    
//    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}

- (void)createScrollview
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HEIGHT/3)];
    //    self.scrollView.backgroundColor=[UIColor whiteColor];
    [self.headerView addSubview:self.scrollView];
    [_scrollView release];
    
    
    self.scrollView.pagingEnabled =YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //设置初始位置
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    
    [self.scrollView alwaysBounceHorizontal];
    // 轮播图时间
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timer) userInfo:nil repeats:YES];
}



- (void)createLaber
{
    self.packageTitle = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 5, HEIGHT / 66.7 , 235, 30)];
    
    self.packageStytle = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2.5, HEIGHT / 14.8 , 75, 25)];
    self.leftPackagePreferential = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 5.7, HEIGHT / 8, 115, 20)];
    self.rightPackagePreferential = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 1.9, HEIGHT / 8, 115, 20)];
    // 文字居中
    self.packageTitle.textAlignment = YES;
    self.packageStytle.textAlignment = YES;
    self.leftPackagePreferential.textAlignment = YES;
    self.rightPackagePreferential.textAlignment = YES;
    
    self.packageTitle.font = [UIFont systemFontOfSize:20];
    self.packageStytle.font = [UIFont systemFontOfSize:16];
    self.leftPackagePreferential.font = [UIFont systemFontOfSize:12];
    self.rightPackagePreferential.font = [UIFont systemFontOfSize:12];
    
    [self.titleView addSubview:self.packageTitle];
    [self.titleView addSubview:self.packageStytle];
    [self.titleView addSubview:self.leftPackagePreferential];
    [self.titleView addSubview:self.rightPackagePreferential];
    [self.packageTitle release];
    [self.packageStytle release];
    [self.leftPackagePreferential release];
    [self.rightPackagePreferential release];
//    self.packageTitle.backgroundColor = [UIColor redColor];
//    self.packageStytle.backgroundColor = [UIColor redColor];
//    self.leftPackagePreferential.backgroundColor = [UIColor redColor];
//    self.rightPackagePreferential.backgroundColor = [UIColor redColor];
    
    
    
    self.leftPackagePreferential.text = @"💓可定金支付";
    self.rightPackagePreferential.text = @"💓全款支付享优惠";
}



- (void)createData
{
    
    
    self.lunboArr = [NSMutableArray array];
//    self.merchantArr =[NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *url = [NSString stringWithFormat:@"http://hunliji.com/v1/api/app/works/%@", self.idStr];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = responseObject;
        self.Dic = dic[@"work"];
        self.bidmerchantdic = self.Dic[@"merchant"];
        self.nameDic = self.Dic[@"merchant"];
        self.addressDic = self.Dic[@"merchant"];
        self.titleStr =self.Dic[@"title"];
        self.styleStr = self.Dic[@"kind"];
        self.packageTitle.text = self.titleStr;
        self.packageStytle.text = self.styleStr;
        self.bidDic = self.Dic[@"merchant"];
//        NSLog(@"%@",self.bidmerchantdic[@"id"]);

        NSArray *arr = self.Dic[@"media_items"];
        for (NSDictionary *tempDic in arr) {
            PackageDetaileModel  *tu= [[PackageDetaileModel alloc]init];
            [tu setValuesForKeysWithDictionary:tempDic];
            
            [self.lunboArr addObject:tu];
        
            [tu release];
        }
//        [self.tableView reloadData];
    
        //------------
   
        AFHTTPRequestOperationManager *manager1 = [AFHTTPRequestOperationManager manager];
        NSString *url1 = [NSString stringWithFormat:@"http://hunliji.com/v1/api/app/merchants/%@.json", self.bidmerchantdic[@"id"]];
        [manager1 GET:url1 parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSDictionary *dic = responseObject;
            self.merchantDic = dic[@"merchant"];
            


            [self.tableView reloadData];
            // 创建Scrollview
            [self createScrollview];
            // 创建轮播图
            [self createscrollview];
            
            [self.hud hide:YES];
//            self.pageCount++;
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            
        }];
        //-------------


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
    
    
}

- (void)timer {
    
    CGPoint point = CGPointMake(self.scrollView.contentOffset.x + WIDTH, 0);
    [self.scrollView setContentOffset:point animated:YES];
    //     图片自动滚动后判断偏移量,如果是最后一张图就直接设置偏移量到第二张图.实现无限滚动
    if (self.scrollView.contentOffset.x == (self.lunbotupicArr.count-1)* WIDTH) {
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
}

- (void)createscrollview
{
    self.lunbotupicArr = [[NSMutableArray array]init];

    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    for (NSInteger i = 0; i < self.lunboArr.count; i++) {
        PackageDetaileModel  *tu = [[PackageDetaileModel alloc]init];
        tu = self.lunboArr[i];
        [self.lunbotupicArr addObject:tu.media_path];
    }
    NSString *lastStr = [self.lunbotupicArr lastObject];
    [self.lunbotupicArr insertObject:lastStr atIndex:0];
    NSString *firstStr = self.lunbotupicArr[1];
    [self.lunbotupicArr insertObject:firstStr atIndex:self.lunbotupicArr.count];
    self.scrollView.contentSize = CGSizeMake(self.lunbotupicArr.count * WIDTH, 0);
    for (NSInteger i = 0; i < self.lunbotupicArr.count; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.lunbotupicArr[i]]];
        
        imageView.frame = CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT/2.2);
        [self.scrollView addSubview:imageView];
        [imageView release];
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
        myLabel.backgroundColor = [UIColor colorWithRed:1.000 green:0.404 blue:0.651 alpha:1.000];
        myLabel.text = @"  商家";
        // 文字居中
//        myLabel.textAlignment = YES;
        return myLabel;
    }else if (section == 1){
        UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
        myLabel1.backgroundColor = [UIColor colorWithRed:1.000 green:0.404 blue:0.651 alpha:1.000];
        myLabel1.text = @"  套餐描述";
//        myLabel1.textAlignment = YES;
        return myLabel1;
    }else if (section == 2){
        UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
        myLabel2.backgroundColor = [UIColor colorWithRed:1.000 green:0.404 blue:0.651 alpha:1.000];
        myLabel2.text = @"------购买须知------";
        myLabel2.textAlignment = YES;
        return myLabel2;
    }else{
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        if ([self.nameDic[@"name"] isKindOfClass:[NSNull class]]) {
            return 0;
        }
        return 1;
    }else if (section == 1){
        if ([self.Dic[@"describe"] isKindOfClass:[NSNull class]]) {
            return 0;
        }
        return 1;
    }else if (section == 2){
        if ([self.Dic[@"purchase_notes"] isKindOfClass:[NSNull class]]) {
            return 0;
        }
        return 1;
    }else{
        return 0;
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    if (section == 0) {
        return 100;
    }else if (section == 1){
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
        // 根据文本计算高度,限制一个文本的最大范围(375,0)
        // 第二个属性作用:指定一个属性来定义对应文本和字体所占用的高度
        CGRect rect = [self.Dic[@"describe"] boundingRectWithSize:CGSizeMake(355, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height +30;
    }else{
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
        // 根据文本计算高度,限制一个文本的最大范围(375,0)
        // 第二个属性作用:指定一个属性来定义对应文本和字体所占用的高度
        CGRect rect = [self.Dic[@"purchase_notes"] boundingRectWithSize:CGSizeMake(355, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + 50;
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    if (section == 0) {
        static NSString *reuse = @"reuse";
        MerchantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        
        if (!cell) {
            cell = [[MerchantTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
//        PackageMerchantDetaileModel *packageMerchantDetaileModel = self.merchantArr[indexPath.row];
//
//        cell.name.text = packageMerchantDetaileModel.name;
//        cell.address.text = packageMerchantDetaileModel.address;
//        cell.shopPic = packageMerchantDetaileModel.logo_path;
        
        
        
        cell.name.text = self.nameDic[@"name"];
        cell.address.text = self.addressDic[@"address"];

        NSString *str = self.addressDic[@"logo_path"];
//        NSString*str = packageMerchantDetaileModel.logo_path;
        
//        [cell.shopPic sd_setImageWithURL:[NSURL URLWithString:str]];
        [cell.shopPic sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"占位图"]];
        
        return cell;
    }else if (section == 1){
        static NSString *reuse = @"reuse1";
        DetailsTableViewCell *detailsTableViewcell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!detailsTableViewcell) {
            detailsTableViewcell = [[DetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
            detailsTableViewcell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        detailsTableViewcell.details.text = self.Dic[@"describe"];
//        detailsTableViewcell.details.backgroundColor = [UIColor yellowColor];
        detailsTableViewcell.details.font = [UIFont systemFontOfSize:15];
        
        
        return detailsTableViewcell;
    }else{
        static NSString *reuse = @"reuse2";
        BuyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[BuyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.purchase_notes.text = self.Dic[@"purchase_notes"];
//        cell.purchase_notes.backgroundColor = [UIColor redColor];
        
        cell.purchase_notes.font = [UIFont systemFontOfSize:15];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    if (section == 0) {
        MerchantDetailsViewController *merchantDetailsViewController = [[MerchantDetailsViewController alloc] init];

        merchantDetailsViewController.merchantIdStr = self.bidDic[@"id"];
        
        merchantDetailsViewController.addressStr = self.addressDic[@"address"];
        merchantDetailsViewController.nameStr = self.nameDic[@"name"];
        merchantDetailsViewController.logoStr = self.addressDic[@"logo_path"];
        

        merchantDetailsViewController.coverStr = self.merchantDic[@"cover_path"];
        [self.navigationController pushViewController:merchantDetailsViewController animated:YES];
        
        MerchantDetailePackageModel *merchant = self.lunboArr[indexPath.row];
        merchantDetailsViewController.merchantUesrId = merchant.bid;
        merchantDetailsViewController.merchantName = merchant.title;
        [merchantDetailsViewController release];
    }
}


//走完viewDidload走这个方法
-(void)viewWillAppear:(BOOL)animated{
    

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
